package com.edu.service.impl;

import com.edu.Constant;
import com.edu.StatusCode;
import com.edu.dao.EduNoticeMapper;
import com.edu.pojo.EduNotice;
import com.edu.service.EduNoticeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 公告处理服务层
 *
 * @author YJF
 */
@Service
public class EduNoticeServiceImpl implements EduNoticeService {

    @Autowired
    private EduNoticeMapper eduNoticeMapper;

    @Override
    public PageInfo<EduNotice> findByPage(Integer page, Integer size, String start, String end) {
        PageHelper.startPage(page, size);
        List<EduNotice> list;
        if (start.length() + end.length() > 15) {
            list = eduNoticeMapper.findByPage(start, end);
        } else {
            list = eduNoticeMapper.selectAll();
        }
        return new PageInfo<>(list);
    }

    @Override
    public int addNts(EduNotice eduNotice) {
        if (eduNotice.getNoticeContent().length() > Constant.TEXTAREA_LIMIT) {
            return StatusCode.OVER_LIMIT;
        }
        eduNotice.setUpdateBy(eduNotice.getCreateBy());
        Date date = new Date();
        eduNotice.setCreateTime(date);
        eduNotice.setUpdateTime(date);
        eduNoticeMapper.insertSelective(eduNotice);
        return StatusCode.OK;
    }

    @Override
    public EduNotice findById(Integer id) {
        return eduNoticeMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateNts(EduNotice eduNotice) {
        eduNotice.setUpdateTime(new Date());
        eduNoticeMapper.updateByPrimaryKeySelective(eduNotice);
        return StatusCode.OK;
    }

    @Override
    public int delNtsById(Integer id) {
        eduNoticeMapper.deleteByPrimaryKey(id);
        return StatusCode.OK;
    }
}
